using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PietschSoft.VE;

namespace SecurityLinkUp
{
    public partial class UserProfile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // if the page is not a postback, show the current data
            if (!Page.IsPostBack)
            {
                DisplayData();
            }
            else
            {
                // page is loading for the first time
                ChangePasswordErrorMessage.Visible = false;
            }

        }

        protected void DisplayData()
        {
            // get the current user
            MembershipUser usr = Membership.GetUser();

            // if user is null, then the user hasnt logged in
            // bail out
            if (usr == null)
            {
                ClearData();
                return;
            }

            // pull the user data and populate the controls
            User u = UserData.GetUser((Guid)usr.ProviderUserKey);
            Username.Text = u.username;
            Email.Text = u.email;
            RealName.Text = u.name;
            JobTitle.Text = u.jobtitle;
            Company.Text = u.company;
            UserPicture.Visible = true;
            UserPicture.ImageUrl = u.picurl;
            allowemailchk.Checked = u.AllowEmail;
            if (u.HasPosition)
            {
                GeoArea.Text = u.geoarea;
                GeoLat.Value = u.geolat.ToString();
                GeoLong.Value = u.geolong.ToString();
                LatLong Position = new LatLong(u.geolat, u.geolong);
                Pushpin p = new PietschSoft.VE.Pushpin(u.uid.ToString(), Position, null, u.geoarea, "");
                Map.Pushpins.Add(p);
                Map.Latitude = u.geolat;
                Map.Longitude = u.geolong;
                Map.Zoom = 10;
                LocationStatusImg.ImageUrl = "~/Images/check.png";
            }
            
        }

        protected void ClearData()
        {
            Username.Text = "";
            Email.Text = "";
            RealName.Text = "";
            JobTitle.Text = "";
            Company.Text = "";
            allowemailchk.Checked = false;
            UserPicture.Visible = false;
            GeoArea.Text = "";
            GeoLat.Value = "";
            GeoLong.Value = "";
            LocationStatusImg.ImageUrl = "~/Images/cross.png";
            Map.Pushpins.Clear();
            Map.Latitude = 0;
            Map.Longitude = 0;
            Map.Zoom = 10;
        }

        protected void SaveBtn_Click(object sender, EventArgs e)
        {
            
            // get the current user
            MembershipUser usr = Membership.GetUser();

            // check we have a current user
            if (usr != null)
            {
                User u = UserData.GetUser((Guid)usr.ProviderUserKey);

                // do we have a new picture - if so, set it
                if (NewPicture.HasFile)
                {
                    string error = u.SavePicture(NewPicture.PostedFile);
                    if (error != "Saved")
                    {
                        ErrorMessage.Text = error;
                        return;
                    }
                }

                // set the data
                u.name = RealName.Text;
                u.jobtitle = JobTitle.Text;
                u.company = Company.Text;
                u.email = Email.Text;
                u.AllowEmail = allowemailchk.Checked;
                // do we have a geo location
                if ((GeoLat.Value != "") && (GeoLong.Value != ""))
                {
                    u.SetGeoData(GeoArea.Text, double.Parse(GeoLat.Value), double.Parse(GeoLong.Value));
                }

                // save the new data
                string err = UserData.SaveUserToDB(u);
                ErrorMessage.Text = err; 

                // if the save went ok, display the new updated info
                if (err == "Saved")
                {
                    DisplayData();
                }
            }
        }

        protected void ConfirmChange_Click(object sender, EventArgs e)
        {
            ChangePasswordErrorMessage.Visible = true;

            if ((CurrentPassword.Text == "") || (NewPassword.Text == "") || (ConfirmPassword.Text == ""))
            {
                ChangePasswordErrorMessage.Text = "Passwords do not match";
                return;
            }

            if (NewPassword.Text != ConfirmPassword.Text)
            {
                
                ChangePasswordErrorMessage.Text = "Passwords do not match";
                return;
            }

            // get the current user
            MembershipUser usr = Membership.GetUser();

            // attempt password change
            bool success = usr.ChangePassword(CurrentPassword.Text, NewPassword.Text);
            if (!success)
            {
                ChangePasswordErrorMessage.Text = "Error! Could not change password";
                return;
            }
            else
            {
                ChangePasswordErrorMessage.Text = "Successfully changed password";
                ChangePasswordPopup.Hide();
            }
        }

        protected void CancelBtn_Click(object sender, EventArgs e)
        {
            ChangePasswordErrorMessage.Visible = false;
            ChangePasswordPopup.Hide();
        }

    }
}
